#!/usr/bin/perl

#get all the partition files in the local nodes, and sort them by rate
#     1. repository data
#     2. user meta data
#     3. system meta data
#     4. user data

BEGIN {push @INC, $ENV{'MY_SPROOT'} . "/scripts/sdk/perl-lib" }

use strict;
use warnings;

use SeaQuest::FcheckItem;
use SeaQuest::Functions qw(:all);

my @volumes = get_local_volumes();
print "Volumes: @volumes\n";

my @files = map {get_volume_files($_)} @volumes;
my %fcheck_items = map { $_->[0] => SeaQuest::FcheckItem->new(@{$_}) } @files;

print "File\tFullpath\tModified\tChecked\n";
print join("\n", values %fcheck_items), "\n";
print "\n";

my @queue;
my %rate;

#generate a priority queue
#it is a list of file name needs to check(not checked yet)

#first, sorted by modified time, the latest to be the last,
@queue = grep {$fcheck_items{$_}->is_dirty() } keys %fcheck_items;

@queue = sort { $fcheck_items{$a}->ts_modified
                 <=> $fcheck_items{$a}->ts_modified
              } @queue;

#then, sorted by repository data, user meta data,
#                system meta data, user data

#we give different data different rate, then sort them by rate

%rate = map { $_ => 10 } @queue; # user data lowest
$rate{$_} = 20 for (get_sys_meta_partitions()); # then sys meta data
$rate{$_} = 30 for (get_user_meta_partitions()); # then user meta data
$rate{$_} = 40 for (get_repo_partitions()); # then repository data


#we want the larger to be first, so, swap the $a and $b
@queue = sort {$rate{$b} <=> $rate{$a}} @queue;

print "$_: $rate{$_}(" . get_tablename($_) . ")\n" for (@queue);
